home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / winsock / ircii2-6.zip / SRC\IRCII-2.6\SOURCE\LIST.C < prev    next >
C/C++ Source or Header  |  1994-12-28  |  4KB  |  217 lines

  1. /*
  2.  * list.c: some generic linked list managing stuff 
  3.  *
  4.  * Written By Michael Sandrof
  5.  *
  6.  * Copyright(c) 1990 
  7.  *
  8.  * See the COPYRIGHT file, or do a HELP IRCII COPYRIGHT 
  9.  */
  10.  
  11. #ifndef lint
  12. static    char    rcsid[] = "@(#)$Id: list.c,v 1.6 1994/07/02 02:32:13 mrg Stab $";
  13. #endif
  14.  
  15. #include "irc.h"
  16.  
  17. #include "list.h"
  18. #include "ircaux.h"
  19.  
  20. /*
  21.  * These have now been made more general. You used to only be able to
  22.  * order these lists by alphabetical order. You can now order them
  23.  * arbitrarily. The functions are still called the same way if you
  24.  * wish to use alphabetical order on the key string, and the old
  25.  * function name now represents a stub function which calls the
  26.  * new with the appropriate parameters.
  27.  *
  28.  * The new function name is the same in each case as the old function
  29.  * name, with the addition of a new parameter, cmp_func, which is
  30.  * used to perform comparisons.
  31.  *
  32.  */
  33.  
  34. int    add_list_strcmp(item1, item2)
  35. List    *item1, *item2;
  36. {
  37.     return my_stricmp(item1->name, item2->name);
  38. }
  39.  
  40. int    list_strcmp(item1, str)
  41. List    *item1;
  42. char    *str;
  43. {
  44.     return my_stricmp(item1->name, str);
  45. }
  46.  
  47. int    list_match(item1, str)
  48. List    *item1;
  49. char    *str;
  50. {
  51.     return wild_match(item1->name, str);
  52. }
  53.  
  54. /*
  55.  * add_to_list: This will add an element to a list.  The requirements for the
  56.  * list are that the first element in each list structure be a pointer to the
  57.  * next element in the list, and the second element in the list structure be
  58.  * a pointer to a character (char *) which represents the sort key.  For
  59.  * example 
  60.  *
  61.  * struct my_list{ struct my_list *next; char *name; <whatever else you want>}; 
  62.  *
  63.  * The parameters are:  "list" which is a pointer to the head of the list. "add"
  64.  * which is a pre-allocated element to be added to the list.  
  65.  */
  66. void    add_to_list_ext(list, add, cmp_func)
  67. List    **list;
  68. List    *add;
  69. int    (*cmp_func)();
  70. {
  71.     List    *tmp,
  72.         *last;
  73.  
  74.     if (!cmp_func)
  75.         cmp_func = add_list_strcmp;
  76.     last = (List *) 0;
  77.     for (tmp = *list; tmp; tmp = tmp->next)
  78.     {
  79.         if (cmp_func(tmp, add) > 0)
  80.             break;
  81.         last = tmp;
  82.     }
  83.     if (last)
  84.         last->next = add;
  85.     else
  86.         *list = add;
  87.     add->next = tmp;
  88. }
  89.  
  90. void    add_to_list(list, add)
  91. List    **list;
  92. List    *add;
  93. {
  94.     add_to_list_ext(list, add, NULL);
  95. }
  96.  
  97.  
  98. /*
  99.  * find_in_list: This looks up the given name in the given list.  List and
  100.  * name are as described above.  If wild is true, each name in the list is
  101.  * used as a wild card expression to match name... otherwise, normal matching
  102.  * is done 
  103.  */
  104. List    *find_in_list_ext(list, name, wild, cmp_func)
  105. List    **list;
  106. char    *name;
  107. int    wild;
  108. int    (*cmp_func)();
  109. {
  110.     List    *tmp;
  111.     int    best_match,
  112.         current_match;
  113.  
  114.     if (!cmp_func)
  115.         cmp_func = wild ? list_match : list_strcmp;
  116.     best_match = 0;
  117.  
  118.     if (wild)
  119.     {
  120.         List    *match = (List *) 0;
  121.  
  122.         for (tmp = *list; tmp; tmp = tmp->next)
  123.         {
  124.             if ((current_match = cmp_func(tmp, name)) > best_match)
  125.             {
  126.                 match = tmp;
  127.                 best_match = current_match;
  128.             }
  129.         }
  130.         return (match);
  131.     }
  132.     else
  133.     {
  134.         for (tmp = *list; tmp; tmp = tmp->next)
  135.             if (cmp_func(tmp, name) == 0)
  136.                 return (tmp);
  137.     }
  138.     return ((List *) 0);
  139. }
  140.  
  141. List    *find_in_list(list, name, wild)
  142. List    **list;
  143. char    *name;
  144. int    wild;
  145. {
  146.     return find_in_list_ext(list, name, wild, NULL);
  147. }
  148.  
  149. /*
  150.  * remove_from_list: this remove the given name from the given list (again as
  151.  * described above).  If found, it is removed from the list and returned
  152.  * (memory is not deallocated).  If not found, null is returned. 
  153.  */
  154. List    *remove_from_list_ext(list, name, cmp_func)
  155. List    **list;
  156. char    *name;
  157. int     (*cmp_func)();
  158. {
  159.     List    *tmp,
  160.         *last;
  161.  
  162.     if (!cmp_func)
  163.         cmp_func = list_strcmp;
  164.     last = (List *) 0;
  165.     for (tmp = *list; tmp; tmp = tmp->next)
  166.     {
  167.         if (cmp_func(tmp, name) == 0)
  168.         {
  169.             if (last)
  170.                 last->next = tmp->next;
  171.             else
  172.                 *list = tmp->next;
  173.             return (tmp);
  174.         }
  175.         last = tmp;
  176.     }
  177.     return ((List *) 0);
  178. }
  179.  
  180. List    *remove_from_list(list, name)
  181. List    **list;
  182. char    *name;
  183. {
  184.     return remove_from_list_ext(list, name, NULL);
  185. }
  186.  
  187.  
  188. /*
  189.  * list_lookup: this routine just consolidates remove_from_list and
  190.  * find_in_list.  I did this cause it fit better with some alread existing
  191.  * code 
  192.  */
  193. List    *list_lookup_ext(list, name, wild, delete, cmp_func)
  194. List    **list;
  195. char    *name;
  196. int    wild;
  197. int    delete;
  198. int    (*cmp_func)();
  199. {
  200.     List    *tmp;
  201.  
  202.     if (delete)
  203.         tmp = remove_from_list_ext(list, name, cmp_func);
  204.     else
  205.         tmp = find_in_list_ext(list, name, wild, cmp_func);
  206.     return (tmp);
  207. }
  208.  
  209. List    *list_lookup(list, name, wild, delete)
  210. List    **list;
  211. char    *name;
  212. int    wild;
  213. int    delete;
  214. {
  215.     return list_lookup_ext(list, name, wild, delete, NULL);
  216. }
  217.